package org.aristotle.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.aristotle.common.utils.AristotleBeanUtils;
import org.aristotle.user.model.dto.UserDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
// 本模块
import org.aristotle.user.model.entity.User;
import org.aristotle.user.model.vo.UserVO;
import org.aristotle.user.mapper.UserMapper;
import org.aristotle.user.service.IUserService;

// 一对多(本类是多)
// 多对一(本类是多)
// 多对多关系
import org.aristotle.user.model.entity.UserRole;
import org.aristotle.user.service.IUserRoleService;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    // 多对一(本类是多)

    // 一对多(本类是一)

    // 多对多关系中间表
    @Autowired
    private IUserRoleService userRoleService;

    @Override
    public Page<UserVO> queryPageList(
        Long id,
        String username,
        String nickname,
        Integer status,
        Integer current,
        Integer size
    ) {
        QueryWrapper<User> queryWrapper = new QueryWrapper();

        if (id != null) {
            queryWrapper.eq("id",id);
        }

            if (!StringUtils.isBlank(username)) {
                queryWrapper.like("username",username);
            }
            if (!StringUtils.isBlank(nickname)) {
                queryWrapper.like("nickname",nickname);
            }
            if (status != null) {
                queryWrapper.eq("status",status);
            }


        Page<User> page = this.page(new Page<User>(current,size), queryWrapper);

        // 转为VO
        Page<UserVO> result = AristotleBeanUtils.transitionBean(page, Page<UserVO>::new);
        result.setRecords(AristotleBeanUtils.copyListProperties(page.getRecords(),UserVO::new));

        result.getRecords().forEach(userVO -> {
            // 多对一(本类是多)
        });
        return result;
    }

    @Override
    public UserVO getById(Long id) {
        User user = this.baseMapper.selectById(id);
        UserVO userVO = AristotleBeanUtils.transitionBean(user, UserVO::new);
        return userVO;
    }

    @Override
    @Transactional
    public Boolean removeById(Long id) {
        /*删除中间表*/
        QueryWrapper<UserRole> queryWrapperUserRole = new QueryWrapper<>();
        queryWrapperUserRole.eq("user_id",id);
        userRoleService.remove(queryWrapperUserRole);
        /*一对多(本类是一)*/

        return super.removeById(id);
    }

    @Override
    public UserDTO getByUsername(String username) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);

        User user = this.getOne(queryWrapper);
        // User转DTO
        UserDTO result = userPOToDTO(user);
        result.setRoleList(userRoleService.getRoleDTOByUserId(result.getId()));
        return result;
    }

    private UserDTO userPOToDTO(User user) {
        UserDTO userDTO = new UserDTO();
        BeanUtils.copyProperties(user,userDTO);
        return userDTO;
    }

}
